const BaseController = require('../core/baseController')


class UserController extends BaseController {
    // 登录接口
    async login() {
        // const res= await
        const username = this.ctx.request.body.username
        const password = this.ctx.request.body.password
        const ret = await this.service.user.findByUsername(username)
        if (ret) {
            if (ret[0].password === password) {
                this.success(ret, '登录成功')
            } else {
                this.failure('密码错误')
            }
        } else {
            this.failure('用户不存在');
        }
    }

    // 退出接口 或者可以直接前端，清除token来退出
    async logOut() {

    }

    // 注册
    // 先看看这个用户是否存在
    // 如果存在
    async register() {
        const username = this.ctx.request.body.username
        const password = this.ctx.request.body.password
        const ret = await this.service.user.findByUsername(username)
        if (ret.length !== 0) {
            this.failure('用户名已经存在')
        } else {
            let params = {
                username,
                password
            }
            const ret = await this.service.user.save(params)
            if (ret) {
                this.success(ret, '注册成功')
            } else {
                this.failure('注册失败')
            }
        }
    }

    async fetchList() {
        const ret = await this.service.user.fetchList()
        if (ret) {
            this.success(ret, '查询成功')
        } else {
            this.failure('查询失败')
        }
    }

    //当前用户
    async fetchUser() {
        this.ctx.body = {
            name: 'Serati Ma',
            avatar: 'https://gw.alipayobjects.com/zos/rmsportal/BiazfanxmamNRoxxVxka.png',
            userid: '00000001',
            email: 'antdesign@alipay.com',
            signature: '海纳百川，有容乃大',
            title: '交互专家',
            group: '蚂蚁金服－某某某事业群－某某平台部－某某技术部－UED',
            tags: [
                {
                    key: '0',
                    label: '很有想法的',
                },
                {
                    key: '1',
                    label: '专注设计',
                },
                {
                    key: '2',
                    label: '辣~',
                },
                {
                    key: '3',
                    label: '大长腿',
                },
                {
                    key: '4',
                    label: '川妹子',
                },
                {
                    key: '5',
                    label: '海纳百川',
                },
            ],
            notifyCount: 12,
            country: 'China',
            geographic: {
                province: {
                    label: '浙江省',
                    key: '330000',
                },
                city: {
                    label: '杭州市',
                    key: '330100',
                },
            },
            address: '西湖区工专路 77 号',
            phone: '0752-268888888',
        }
    }


}

module.exports = UserController
